package com.dartfar.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dartfar.dictionary.model.Dictcatalog;
import com.dartfar.dictionary.model.Dictionary;
import com.dartfar.dictionary.service.DictcatalogService;
import com.dartfar.dictionary.service.DictionaryService;


/**
 * 数据字典工厂
 * @author 王琦
 *
 */
public class DictionaryFactory {
	
	@Resource
	private DictcatalogService dictcatalogService;
	
	@Resource
	private DictionaryService dictionaryService;
	
	public DictionaryFactory(){  
        super();  
    }  
	
	private static WebApplicationContext springContext;  
	
    public static ServletContext servletContext;//这个是为了取servletContext  
    
    private static DictionaryFactory instance;   
    
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    
    public static DictionaryFactory getInstance(){  
        springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);  
        if(null == instance)  
            instance = (DictionaryFactory)springContext.getBean("dictionaryFactory");  
        return instance;  
          
    }  
    
    public static HashMap<String, HashMap> dictMap = new HashMap<String, HashMap>();

    public void initDictionaryFactory(){
    	
    	System.out.println(sdf.format(new Date()) +  ",187  INFO [main] DictionaryFactory:18 - Initialize Dictionary Data...");
    	//获取字典目录
    	List<Dictcatalog> list = dictcatalogService.getList();
    	//遍历字典目录
    	for(Iterator<Dictcatalog> i =  list.iterator();i.hasNext();){
    		Dictcatalog catalog = i.next();
    		HashMap dMap = new HashMap();
    		List<Dictionary> dictList = dictionaryService.getListByDictCatalog(catalog.getId()); 
    		for(Iterator<Dictionary> d =  dictList.iterator();d.hasNext();){
    			Dictionary dict = d.next();
    			dMap.put(dict.getD_key(), dict.getD_word());
    		}
    		dictMap.put(catalog.getCode(), dMap);
    	}
    	
    }
    
    /** 
     * 重新加载字典表数据！ 
     * */  
    public void reloadDictMap()  
    {  
    	System.out.println(sdf.format(new Date()) +  ",187  INFO [main] DictionaryFactory:18 - Dictionary Data Clear...");
        dictMap.clear();  
    	System.out.println(sdf.format(new Date()) +  ",187  INFO [main] DictionaryFactory:18 - Dictionary Data Clear Successfully");
        initDictionaryFactory();  
        System.out.println(sdf.format(new Date()) +  ",187  INFO [main] DictionaryFactory:18 - Dictionary Data Reload Successfully");   
    }

    
	public static HashMap<String, HashMap> getDictMap() {
		return dictMap;
	}

	public static void setDictMap(HashMap<String, HashMap> dictMap) {
		DictionaryFactory.dictMap = dictMap;
	}


    
    
}
